{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 处理图像"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(720, 1280, 3)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator.DESKTOP-735C72S\\AppData\\Local\\Temp\\ipykernel_7980\\829484506.py:4: DeprecationWarning: Starting with ImageIO v3 the behavior of this function will switch to that of iio.v3.imread. To keep the current behavior (and make this warning disappear) use `import imageio.v2 as imageio` or call `imageio.v2.imread` directly.\n",
      "  img_arr = imageio.imread('F:/深度学习实战资料/本书代码文件/dlwpt-code-master 2/dlwpt-code-master/data/p1ch4/image-dog/bobby.jpg')\n"
     ]
    }
   ],
   "source": [
    "## 加载图像文件\n",
    "import imageio\n",
    "from PIL import Image\n",
    "img_arr = imageio.imread('F:/深度学习实战资料/本书代码文件/dlwpt-code-master 2/dlwpt-code-master/data/p1ch4/image-dog/bobby.jpg')\n",
    "print(img_arr.shape)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-11T07:33:42.144595300Z",
     "start_time": "2023-10-11T07:33:42.120596700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "torch.Size([3, 720, 1280])"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 改变布局\n",
    "import torch\n",
    "img = torch.from_numpy(img_arr)\n",
    "out = img.permute(2,0,1)\n",
    "out.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-11T07:35:17.414164900Z",
     "start_time": "2023-10-11T07:34:48.941317800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator.DESKTOP-735C72S\\AppData\\Local\\Temp\\ipykernel_7980\\3926473135.py:10: DeprecationWarning: Starting with ImageIO v3 the behavior of this function will switch to that of iio.v3.imread. To keep the current behavior (and make this warning disappear) use `import imageio.v2 as imageio` or call `imageio.v2.imread` directly.\n",
      "  img_arr = imageio.imread(os.path.join(data_dir,filename))\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "batch_size=3\n",
    "batch = torch.zeros(batch_size,3,256,256,dtype=torch.uint8)\n",
    "\n",
    "data_dir ='F:/深度学习实战资料/本书代码文件/dlwpt-code-master 2/dlwpt-code-master/data/p1ch4/image-cats'\n",
    "import os\n",
    "filenames = [name for name in os.listdir(data_dir)\n",
    "              if os.path.splitext(name)[-1]=='.png']\n",
    "for i,filename in enumerate(filenames):\n",
    "    img_arr = imageio.imread(os.path.join(data_dir,filename))\n",
    "    img_t = torch.from_numpy(img_arr)\n",
    "    img_t = img_t.permute(2,0,1)\n",
    "    img_t = img_t[:3]\n",
    "    batch[i] = img_t"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-11T07:49:49.531281300Z",
     "start_time": "2023-10-11T07:49:49.398290200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "torch.Size([3, 3, 256, 256])"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "batch.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-11T07:50:10.759782900Z",
     "start_time": "2023-10-11T07:50:10.744771100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [],
   "source": [
    "## 数据归一化\n",
    "batch = batch.float()\n",
    "n_channels = batch.shape[1]\n",
    "for c in range(n_channels):\n",
    "    mean = torch.mean(batch[:,c])\n",
    "    std = torch.std(batch[:,c])\n",
    "    batch[:,c] = (batch[:,c]-mean)/std"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-11T07:57:41.723596900Z",
     "start_time": "2023-10-11T07:57:41.304616200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Reading DICOM (examining files): 99/99 files (100.0%)\n",
      "  Found 1 correct series.\n",
      "Reading DICOM (loading data): 99/99  (100.0%)\n"
     ]
    },
    {
     "data": {
      "text/plain": "(99, 512, 512)"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import imageio\n",
    "dir_path = 'F:/深度学习实战资料/本书代码文件/dlwpt-code-master 2/dlwpt-code-master/data/p1ch4/volumetric-dicom/2-LUNG 3.0  B70f-04083'\n",
    "vol_arr = imageio.volread(dir_path,'DICOM')\n",
    "vol_arr.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-11T08:18:14.244457200Z",
     "start_time": "2023-10-11T08:18:10.475197600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "torch.Size([1, 99, 512, 512])"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vol = torch.from_numpy(vol_arr).float()\n",
    "vol = torch.unsqueeze(vol,0)\n",
    "vol.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-11T08:19:14.327511700Z",
     "start_time": "2023-10-11T08:19:14.217517Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
